﻿using ArtMath.Component.Point;
using ArtMath.Component;
using System;
using ArtMath.Attribute;

namespace ArtMath.Parameter
{
    /// <summary>
    /// 两点距离
    /// </summary>
    [Serializable]
    public class TwoPointsDistanceParameter : CalculatedParameter
    {
        PointBase pt1, pt2;
        public TwoPointsDistanceParameter(SketchPad pad, string name, PointBase pt1, PointBase pt2) : base(pad, name, ToolTypeName.ldjl)
        {
            Point1 = pt1;
            Point2 = pt2;
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}1", "Point")]
        public PointBase Point1
        {
            get { return pt1; }
            set
            {
                if (pt1 is object)
                {
                    pt1.CalculatedParameters.Remove(this);
                }
                pt1 = value;
                if (pt1 is object)
                {
                    pt1.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("{0}2", "Point")]
        public PointBase Point2
        {
            get { return pt2; }
            set
            {
                if (pt2 is object)
                {
                    pt2.CalculatedParameters.Remove(this);
                }
                pt2 = value;
                if (pt2 is object)
                {
                    pt2.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        public override double GetNewValue()
        {
            if (Point1 is null || Point2 is null)
            {
                return 0;
            }
            return Point1.NewXY.DistanceTo(Point2.NewXY);
        }
        public override void Delete(bool recoverable = false)
        {
            base.Delete(recoverable);
            if (recoverable)
            {

            }
            else
            {
                Point1 = null;
                Point2 = null;
            }
        }
        protected override void ForEachRelatedObject(Predicate<Base> action)
        {
            if (action == null) return;
            if (!action(Point1)) return;
            if (!action(Point2)) return;
        }
        public override void ChangePoint(PointBase oldPoint, PointBase newPoint)
        {
            if (ReferenceEquals(Point1, oldPoint))
            {
                Point1 = newPoint;
            }
            else if (ReferenceEquals(Point2, oldPoint))
            {
                Point2 = newPoint;
            }
            else
            {
                base.ChangePoint(oldPoint, newPoint);
            }
        }
    }
}
